let http = require("http");
let fs = require("fs");
require("./buffer-split");


/*
 
 
 ------WebKitFormBoundaryjvAIjT3JzYSCJEnB
Content-Disposition: form-data; name="user"

admin
------WebKitFormBoundaryjvAIjT3JzYSCJEnB
Content-Disposition: form-data; name="pass"

123456
------WebKitFormBoundaryjvAIjT3JzYSCJEnB
Content-Disposition: form-data; name="f1"; filename="1.txt"
Content-Type: text/plain

aaabbb
------WebKitFormBoundaryjvAIjT3JzYSCJEnB--
*/
http.createServer((req,res)=>{
  
  let str = [];
  req.on("data",data=>{
    str.push(data);
  });
  req.on("end",()=>{
    let buffer = Buffer.concat(str);
    let arr = buffer.split("\r\n");
    if(req.headers['content-type']){
      arr.map((a,index)=>console.log(a.toString(),index));
      fs.writeFile(`1111111`, arr[12], err=>{
        if(err){
          console.log('文件写入失败', err);
        }else{
          console.log('文件写入成功');
        }
      });
    }
    res.end();
  });
  
}).listen(8080);